<!-- 扇形图页面地图 -->
<template>

  <div>
    <button @click="goBack" class="backButton"></button>
    <transition name="fade">
      <div v-if="loading" class="loading">
        <div class="loading-spinner"></div>
      </div>
    </transition>
    <div ref="pieChart" style="width: 100%;height: 400px;"></div>
  </div>
</template>

<script>
import ForceMap from './mapForce.vue'
import RankList from "./rankList.vue";
import { getForceInfo } from '@/server/screen-data'
import * as echarts from 'echarts';
export default {
  data() {
    return {
      loading: true, // 是否显示加载动画
      UserData: [],
      UserForce: [],
      UserList:[],
      a: 0,
      b: 0,
      c: 0,
      d: 0,
      e: 0,
      f: 0
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    getlist() {
      getForceInfo().then((res) => {
        if (res.status == 200) {
          this.UserData = res.data.data.cityInfoList
          this.UserData.forEach(city => {
            city.list.forEach(user => {
              this.UserList.push(user)
              this.UserForce = this.UserList.map(item => item.score)
            })
          })
          // 请求成功后隐藏加载动画
          this.loading = false;
          this.countForce()
          this.renderChart(); // 绘制扇形图
        }
      }).catch(() => {

      })
    },
    countForce() {
      for (let i = 0; i < this.UserForce.length; i++) {
        if (this.UserForce[i] <= 200) {
          this.a++
        } else if (this.UserForce[i] <= 400) {
          this.b++
        } else if (this.UserForce[i] <= 600) {
          this.c++
        } else if (this.UserForce[i] <= 800) {
          this.d++
        } else if (this.UserForce[i] <= 1000) {
          this.e++
        } else {
          this.f++
        }
      }
    },
    renderChart() {
      const chartDom = this.$refs.pieChart;
      const myChart = echarts.init(chartDom);
      const option = {
        title: {
          text: '博主月度原力值统计表', // 主标题
          subtext: '', // 副标题
          x: 'center' // x轴方向对齐方式
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 400,
          top:200,
          data:['原力值小于200','原力值在200到400之间','原力值在400到600之间','原力值在600到800之间','原力值在800到1000之间','原力值大于1000']
        },

        series: [
          {
            name: '原力值分布',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '30',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data:[
              {value:this.a,name:"原力值小于200"},
              {value:this.b,name:"原力值在200到400之间"},
              {value:this.c,name:"原力值在400到600之间"},
              {value:this.d,name:"原力值在600到800之间"},
              {value:this.e,name:"原力值在800到1000之间"},
              {value:this.f,name:"原力值大于1000"},

            ]
          }
        ]
      };

      myChart.setOption(option);
    }
  },
  created() {
    this.getlist()
  }
}
</script>
<style>
.loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>